<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Manipulating GraphData &mdash; Graph4NLP v0.4.1 documentation</title><link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  <script id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <script src="../../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Batching" href="batching.html" />
    <link rel="prev" title="Constructing GraphData" href="construct.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
            <a href="../../index.html" class="icon icon-home"> Graph4NLP
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <p class="caption"><span class="caption-text">Get Started</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../welcome/installation.html">Install Graph4NLP</a></li>
</ul>
<p class="caption"><span class="caption-text">User Guide</span></p>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="../graphdata.html">Chapter 1. Graph Data</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="construct.html">Constructing GraphData</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Manipulating GraphData</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#features">Features</a></li>
<li class="toctree-l3"><a class="reference internal" href="#attributes">Attributes</a></li>
<li class="toctree-l3"><a class="reference internal" href="#features-vs-attributes">Features vs. Attributes</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="batching.html">Batching</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../dataset.html">Chapter 2. Dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../construction.html">Chapter 3. Graph Construction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../gnn.html">Chapter 4. Graph Encoder</a></li>
<li class="toctree-l1"><a class="reference internal" href="../decoding.html">Chapter 5. Decoder</a></li>
<li class="toctree-l1"><a class="reference internal" href="../classification.html">Chapter 6. Classification</a></li>
<li class="toctree-l1"><a class="reference internal" href="../evaluation.html">Chapter 7. Evaluations and Loss components</a></li>
</ul>
<p class="caption"><span class="caption-text">Module API references</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../modules/data.html">graph4nlp.data</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/datasets.html">graph4nlp.datasets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/graph_construction.html">graph4nlp.graph_construction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/graph_embedding.html">graph4nlp.graph_embedding</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/prediction.html">graph4nlp.prediction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/loss.html">graph4nlp.loss</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/evaluation.html">graph4nlp.evaluation</a></li>
</ul>
<p class="caption"><span class="caption-text">Tutorials</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/text_classification.html">Text Classification Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/semantic_parsing.html">Semantic Parsing Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/math_word_problem.html">Math Word Problem Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/knowledge_graph_completion.html">Knowledge Graph Completion Tutorial</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">Graph4NLP</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
          <li><a href="../graphdata.html">Chapter 1. Graph Data</a> &raquo;</li>
      <li>Manipulating GraphData</li>
      <li class="wy-breadcrumbs-aside">
            <a href="../../_sources/guide/graphdata/manipulate.rst.txt" rel="nofollow"> View page source</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <div class="section" id="manipulating-graphdata">
<span id="guide-manipulate"></span><h1>Manipulating GraphData<a class="headerlink" href="#manipulating-graphdata" title="Permalink to this headline">¶</a></h1>
<p>After constructing a GraphData instance and adding several nodes and edges to it, the next job is to attach some useful
information to it for further processing. In <code class="docutils literal notranslate"><span class="pre">GraphData</span></code>, there are two types of information related to the nodes and
edges, namely <cite>features</cite> and <cite>attributes</cite>.</p>
<div class="section" id="features">
<h2>Features<a class="headerlink" href="#features" title="Permalink to this headline">¶</a></h2>
<p>Features are PyTorch tensors designated for each node or edge. To access features, users may call
<code class="docutils literal notranslate"><span class="pre">GraphData.nodes[node_index].features</span></code> or <code class="docutils literal notranslate"><span class="pre">GraphData.edges[index].features</span></code>.
These methods return the features of the specified nodes or edges in a dictionary representation, where the keys are the
names of the feature and the values are the corresponding tensors.
Alternatively, if the user wants to access features from the whole-graph level, the <code class="docutils literal notranslate"><span class="pre">GraphData.node_features</span></code> and
<code class="docutils literal notranslate"><span class="pre">GraphData.edge_features</span></code> interfaces do the exact job.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">g</span> <span class="o">=</span> <span class="n">GraphData</span><span class="p">()</span>
<span class="n">g</span><span class="o">.</span><span class="n">add_nodes</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>

<span class="c1"># Note that the first dimension of features represent the number of instances(nodes/edges).</span>
<span class="c1"># Any manipulation to the features should keep the match between the number of instances and the dimension size</span>
<span class="c1"># An invalid example</span>
<span class="n">g</span><span class="o">.</span><span class="n">node_features</span><span class="p">[</span><span class="s1">&#39;node_feat&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">randn</span><span class="p">((</span><span class="mi">9</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>
<span class="o">&gt;&gt;&gt;</span> <span class="k">raise</span> <span class="n">SizeMisatchError</span>

<span class="n">g</span><span class="o">.</span><span class="n">node_features</span><span class="p">[</span><span class="s1">&#39;node_feat&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">randn</span><span class="p">((</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>
<span class="n">g</span><span class="o">.</span><span class="n">node_features</span><span class="p">[</span><span class="s1">&#39;zero&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="n">g</span><span class="o">.</span><span class="n">node_features</span><span class="p">[</span><span class="s1">&#39;idx&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">torch</span><span class="o">.</span><span class="n">long</span><span class="p">)</span>
<span class="n">g</span><span class="o">.</span><span class="n">node_features</span>
<span class="o">&gt;&gt;&gt;</span> <span class="p">{</span><span class="s1">&#39;node_feat&#39;</span><span class="p">:</span> <span class="n">tensor</span><span class="p">([[</span><span class="o">-</span><span class="mf">2.2053</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.9236</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.4437</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.7142</span><span class="p">,</span>  <span class="mf">1.5309</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.5863</span><span class="p">,</span>  <span class="mf">0.6002</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.6847</span><span class="p">,</span>
      <span class="mf">1.3772</span><span class="p">,</span>  <span class="mf">0.1066</span><span class="p">],</span>
    <span class="p">[</span> <span class="mf">0.8875</span><span class="p">,</span>  <span class="mf">1.7674</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.0354</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.7681</span><span class="p">,</span> <span class="o">-</span><span class="mf">2.6256</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.3399</span><span class="p">,</span> <span class="o">-</span><span class="mf">2.3798</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.7418</span><span class="p">,</span>
      <span class="mf">1.2901</span><span class="p">,</span>  <span class="mf">0.6641</span><span class="p">],</span>
    <span class="p">[</span><span class="o">-</span><span class="mf">1.5530</span><span class="p">,</span>  <span class="mf">0.9147</span><span class="p">,</span>  <span class="mf">0.0618</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.0879</span><span class="p">,</span>  <span class="mf">1.0005</span><span class="p">,</span>  <span class="mf">1.2638</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.4481</span><span class="p">,</span>  <span class="mf">1.2975</span><span class="p">,</span>
     <span class="o">-</span><span class="mf">0.0304</span><span class="p">,</span>  <span class="mf">0.8707</span><span class="p">],</span>
    <span class="p">[</span><span class="o">-</span><span class="mf">0.3448</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.7484</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.0194</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.5096</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.2596</span><span class="p">,</span>  <span class="mf">0.1056</span><span class="p">,</span>  <span class="mf">1.1560</span><span class="p">,</span>  <span class="mf">0.3463</span><span class="p">,</span>
     <span class="o">-</span><span class="mf">0.1986</span><span class="p">,</span>  <span class="mf">0.9243</span><span class="p">],</span>
    <span class="p">[</span><span class="o">-</span><span class="mf">0.3555</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.7062</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.0459</span><span class="p">,</span>  <span class="mf">0.1305</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.1338</span><span class="p">,</span>  <span class="mf">1.2952</span><span class="p">,</span>  <span class="mf">1.2923</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.5740</span><span class="p">,</span>
     <span class="o">-</span><span class="mf">0.5492</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.2497</span><span class="p">],</span>
    <span class="p">[</span><span class="o">-</span><span class="mf">0.7125</span><span class="p">,</span>  <span class="mf">1.2456</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.2136</span><span class="p">,</span>  <span class="mf">0.8562</span><span class="p">,</span>  <span class="mf">1.8037</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.0379</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.6863</span><span class="p">,</span>  <span class="mf">1.2693</span><span class="p">,</span>
     <span class="o">-</span><span class="mf">0.1980</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.3153</span><span class="p">],</span>
    <span class="p">[</span> <span class="mf">0.4099</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.8295</span><span class="p">,</span>  <span class="mf">0.6984</span><span class="p">,</span>  <span class="mf">0.4125</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.8396</span><span class="p">,</span>  <span class="mf">1.8205</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.1458</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.0837</span><span class="p">,</span>
     <span class="o">-</span><span class="mf">0.2388</span><span class="p">,</span>  <span class="mf">0.0552</span><span class="p">],</span>
    <span class="p">[</span><span class="o">-</span><span class="mf">1.4068</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.9334</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.0367</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.3297</span><span class="p">,</span>  <span class="mf">1.0705</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.5606</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.0458</span><span class="p">,</span>  <span class="mf">0.1358</span><span class="p">,</span>
      <span class="mf">1.3042</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.8282</span><span class="p">],</span>
    <span class="p">[</span> <span class="mf">0.7764</span><span class="p">,</span>  <span class="mf">0.1442</span><span class="p">,</span>  <span class="mf">1.6043</span><span class="p">,</span>  <span class="mf">0.1052</span><span class="p">,</span>  <span class="mf">1.4648</span><span class="p">,</span> <span class="o">-</span><span class="mf">2.1791</span><span class="p">,</span>  <span class="mf">0.6740</span><span class="p">,</span>  <span class="mf">0.2858</span><span class="p">,</span>
      <span class="mf">0.0482</span><span class="p">,</span>  <span class="mf">0.9058</span><span class="p">],</span>
    <span class="p">[</span><span class="o">-</span><span class="mf">1.5054</span><span class="p">,</span>  <span class="mf">0.8992</span><span class="p">,</span>  <span class="mf">0.0893</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.2325</span><span class="p">,</span>  <span class="mf">0.8888</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.2222</span><span class="p">,</span>  <span class="mf">2.0569</span><span class="p">,</span>  <span class="mf">0.0218</span><span class="p">,</span>
      <span class="mf">1.5519</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.8234</span><span class="p">]]),</span>
    <span class="s1">&#39;node_emb&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
    <span class="s1">&#39;zero&#39;</span><span class="p">:</span> <span class="n">tensor</span><span class="p">([</span><span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">]),</span>
    <span class="s1">&#39;idx&#39;</span><span class="p">:</span> <span class="n">tensor</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">])}</span>
</pre></div>
</div>
<p>Note that there are some reserved keys for features, which are initialized to be <code class="docutils literal notranslate"><span class="pre">None</span></code>. In node features the reserved keys
are <cite>node_feat</cite> and <cite>node_emb</cite>. In edge features the reserved keys are <cite>edge_feat</cite>, <cite>edge_emb</cite> and <cite>edge_weight</cite>.
Users are encouraged to use these keys as common feature names.
This means that the feature dictionary of an empty graph will have these items with the corresponding values being <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
<p>Another thing to notice is that when adding new nodes or edges to a graph whose features are already set(the value is not
None), zero padding will be performed on the newly added instances.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">g</span><span class="o">.</span><span class="n">add_nodes</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">g</span><span class="o">.</span><span class="n">node_features</span>     <span class="c1"># Zero padding is performed</span>
<span class="o">&gt;&gt;&gt;</span> <span class="p">{</span><span class="s1">&#39;node_feat&#39;</span><span class="p">:</span> <span class="n">tensor</span><span class="p">([[</span><span class="o">-</span><span class="mf">2.2053</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.9236</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.4437</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.7142</span><span class="p">,</span>  <span class="mf">1.5309</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.5863</span><span class="p">,</span>  <span class="mf">0.6002</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.6847</span><span class="p">,</span>
      <span class="mf">1.3772</span><span class="p">,</span>  <span class="mf">0.1066</span><span class="p">],</span>
    <span class="p">[</span> <span class="mf">0.8875</span><span class="p">,</span>  <span class="mf">1.7674</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.0354</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.7681</span><span class="p">,</span> <span class="o">-</span><span class="mf">2.6256</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.3399</span><span class="p">,</span> <span class="o">-</span><span class="mf">2.3798</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.7418</span><span class="p">,</span>
      <span class="mf">1.2901</span><span class="p">,</span>  <span class="mf">0.6641</span><span class="p">],</span>
    <span class="p">[</span><span class="o">-</span><span class="mf">1.5530</span><span class="p">,</span>  <span class="mf">0.9147</span><span class="p">,</span>  <span class="mf">0.0618</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.0879</span><span class="p">,</span>  <span class="mf">1.0005</span><span class="p">,</span>  <span class="mf">1.2638</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.4481</span><span class="p">,</span>  <span class="mf">1.2975</span><span class="p">,</span>
     <span class="o">-</span><span class="mf">0.0304</span><span class="p">,</span>  <span class="mf">0.8707</span><span class="p">],</span>
    <span class="p">[</span><span class="o">-</span><span class="mf">0.3448</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.7484</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.0194</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.5096</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.2596</span><span class="p">,</span>  <span class="mf">0.1056</span><span class="p">,</span>  <span class="mf">1.1560</span><span class="p">,</span>  <span class="mf">0.3463</span><span class="p">,</span>
     <span class="o">-</span><span class="mf">0.1986</span><span class="p">,</span>  <span class="mf">0.9243</span><span class="p">],</span>
    <span class="p">[</span><span class="o">-</span><span class="mf">0.3555</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.7062</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.0459</span><span class="p">,</span>  <span class="mf">0.1305</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.1338</span><span class="p">,</span>  <span class="mf">1.2952</span><span class="p">,</span>  <span class="mf">1.2923</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.5740</span><span class="p">,</span>
     <span class="o">-</span><span class="mf">0.5492</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.2497</span><span class="p">],</span>
    <span class="p">[</span><span class="o">-</span><span class="mf">0.7125</span><span class="p">,</span>  <span class="mf">1.2456</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.2136</span><span class="p">,</span>  <span class="mf">0.8562</span><span class="p">,</span>  <span class="mf">1.8037</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.0379</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.6863</span><span class="p">,</span>  <span class="mf">1.2693</span><span class="p">,</span>
     <span class="o">-</span><span class="mf">0.1980</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.3153</span><span class="p">],</span>
    <span class="p">[</span> <span class="mf">0.4099</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.8295</span><span class="p">,</span>  <span class="mf">0.6984</span><span class="p">,</span>  <span class="mf">0.4125</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.8396</span><span class="p">,</span>  <span class="mf">1.8205</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.1458</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.0837</span><span class="p">,</span>
     <span class="o">-</span><span class="mf">0.2388</span><span class="p">,</span>  <span class="mf">0.0552</span><span class="p">],</span>
    <span class="p">[</span><span class="o">-</span><span class="mf">1.4068</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.9334</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.0367</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.3297</span><span class="p">,</span>  <span class="mf">1.0705</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.5606</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.0458</span><span class="p">,</span>  <span class="mf">0.1358</span><span class="p">,</span>
      <span class="mf">1.3042</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.8282</span><span class="p">],</span>
    <span class="p">[</span> <span class="mf">0.7764</span><span class="p">,</span>  <span class="mf">0.1442</span><span class="p">,</span>  <span class="mf">1.6043</span><span class="p">,</span>  <span class="mf">0.1052</span><span class="p">,</span>  <span class="mf">1.4648</span><span class="p">,</span> <span class="o">-</span><span class="mf">2.1791</span><span class="p">,</span>  <span class="mf">0.6740</span><span class="p">,</span>  <span class="mf">0.2858</span><span class="p">,</span>
      <span class="mf">0.0482</span><span class="p">,</span>  <span class="mf">0.9058</span><span class="p">],</span>
    <span class="p">[</span><span class="o">-</span><span class="mf">1.5054</span><span class="p">,</span>  <span class="mf">0.8992</span><span class="p">,</span>  <span class="mf">0.0893</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.2325</span><span class="p">,</span>  <span class="mf">0.8888</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.2222</span><span class="p">,</span>  <span class="mf">2.0569</span><span class="p">,</span>  <span class="mf">0.0218</span><span class="p">,</span>
      <span class="mf">1.5519</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.8234</span><span class="p">],</span>
    <span class="p">[</span> <span class="mf">0.0000</span><span class="p">,</span>  <span class="mf">0.0000</span><span class="p">,</span>  <span class="mf">0.0000</span><span class="p">,</span>  <span class="mf">0.0000</span><span class="p">,</span>  <span class="mf">0.0000</span><span class="p">,</span>  <span class="mf">0.0000</span><span class="p">,</span>  <span class="mf">0.0000</span><span class="p">,</span>  <span class="mf">0.0000</span><span class="p">,</span>
      <span class="mf">0.0000</span><span class="p">,</span>  <span class="mf">0.0000</span><span class="p">]]),</span>
    <span class="s1">&#39;node_emb&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
    <span class="s1">&#39;zero&#39;</span><span class="p">:</span> <span class="n">tensor</span><span class="p">([</span><span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">]),</span>
    <span class="s1">&#39;idx&#39;</span><span class="p">:</span> <span class="n">tensor</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">0</span><span class="p">])}</span>
</pre></div>
</div>
</div>
<div class="section" id="attributes">
<h2>Attributes<a class="headerlink" href="#attributes" title="Permalink to this headline">¶</a></h2>
<p>Another important attached information is <cite>attribute</cite>. Like <cite>features</cite>, <cite>attribute</cite> is related to each node/edge
instance and is basically a list of dictionaries. The list index corresponds to the node/edge index and the dictionary
at each position stands for the corresponding attributes of that instance.
Essentially, <cite>attribute</cite> is designed to make up for the limit of <cite>features</cite> in storing arbitrary objects. The reserved
keys are <cite>node_attr</cite> for node attributes and <cite>edge_attr</cite> for edge attributes.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">g</span> <span class="o">=</span> <span class="n">GraphData</span><span class="p">()</span>
<span class="n">g</span><span class="o">.</span><span class="n">add_nodes</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>  <span class="c1"># Add 2 nodes to an empty graph</span>
<span class="n">g</span><span class="o">.</span><span class="n">node_attributes</span>
<span class="o">&gt;&gt;&gt;</span> <span class="p">[{</span><span class="s1">&#39;node_attr&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">},</span> <span class="p">{</span><span class="s1">&#39;node_attr&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">}]</span>
<span class="n">g</span><span class="o">.</span><span class="n">node_attributes</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="s1">&#39;node_attr&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;hello&#39;</span>
<span class="n">g</span><span class="o">.</span><span class="n">node_attributes</span>
<span class="o">&gt;&gt;&gt;</span> <span class="p">[{</span><span class="s1">&#39;node_attr&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">},</span> <span class="p">{</span><span class="s1">&#39;node_attr&#39;</span><span class="p">:</span> <span class="s1">&#39;hello&#39;</span><span class="p">}]</span>
</pre></div>
</div>
</div>
<div class="section" id="features-vs-attributes">
<h2>Features vs. Attributes<a class="headerlink" href="#features-vs-attributes" title="Permalink to this headline">¶</a></h2>
<p>To make it clear, in this subsection we compare the differences between features and attributes in order for users to
better utilize them.</p>
<ol class="arabic simple">
<li><p>Types of storage</p></li>
</ol>
<p><code class="docutils literal notranslate"><span class="pre">features</span></code> store only the numerical feature objects. In current version these data are PyTorch tensors. The shape of these
tensor data should be consistent with the number of nodes/edges in the graph. Specifically, the first dimension
of the tensor data corresponds to the number of instances. For example, in a graph with 10 nodes and 20 edges, the shape
of any node feature tensor should be [10, <a href="#id1"><span class="problematic" id="id2">*</span></a>] and [20, <a href="#id3"><span class="problematic" id="id4">*</span></a>] for any edge feature.</p>
<p>On the other hand, <code class="docutils literal notranslate"><span class="pre">attributes</span></code> store arbitrary type of data. The data can be of any type and do not necessarily need
to have a <code class="docutils literal notranslate"><span class="pre">shape</span></code>.</p>
<ol class="arabic simple" start="2">
<li><p>Order of access</p></li>
</ol>
<p>Both <code class="docutils literal notranslate"><span class="pre">features</span></code> and <code class="docutils literal notranslate"><span class="pre">attributes</span></code> have two levels of keys: <em>names</em> and <em>indices</em>. <code class="docutils literal notranslate"><span class="pre">features</span></code> are implemented as a
dictionary where the keys are strings and values are tensors. Therefore, the first level of key is the feature names.
In this way, the second level of keys are just direct access to the corresponding PyTorch tensors.</p>
<p>On the other hand, <code class="docutils literal notranslate"><span class="pre">attributes</span></code> are implemented as a list of dictionaries, where the list indices are the node indices.
Therefore, when accessing attributes, users should use the index first.</p>
</div>
</div>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="construct.html" class="btn btn-neutral float-left" title="Constructing GraphData" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="batching.html" class="btn btn-neutral float-right" title="Batching" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2020, Graph4AI Group.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>